package org.szgb.console.cxht.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.cxht.entity.*;
import org.szgb.console.cxht.mapper.CxhtFormMapper;
import org.szgb.console.cxht.mapper.CxhtLocationFormItemMapper;
import org.szgb.console.cxht.mapper.CxhtLocationFormMapper;
import org.szgb.console.sys.line.entity.SysLineLocationSystemEquipment;
import org.szgb.console.sys.line.mapper.SysLineLocationSystemEquipmentMapper;
import org.szgb.core.base.entity.BeanEntity;
import org.szgb.core.base.service.BaseService;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CxhtLocationFormService extends BaseService<CxhtLocationFormMapper, CxhtLocationForm> {
    private final CxhtFormMapper cxhtFormMapper;
    private final SysLineLocationSystemEquipmentMapper sysLineLocationSystemEquipmentMapper;
    private final CxhtLocationFormItemMapper cxhtLocationFormItemMapper;

    @Autowired
    public CxhtLocationFormService(CxhtLocationFormMapper cxhtLocationFormMapper, CxhtFormMapper cxhtFormMapper,
                                   SysLineLocationSystemEquipmentMapper sysLineLocationSystemEquipmentMapper,
                                   CxhtLocationFormItemMapper cxhtLocationFormItemMapper) {
        super(cxhtLocationFormMapper);
        this.cxhtFormMapper = cxhtFormMapper;
        this.sysLineLocationSystemEquipmentMapper = sysLineLocationSystemEquipmentMapper;
        this.cxhtLocationFormItemMapper = cxhtLocationFormItemMapper;
    }

    @Transactional()
    public void issue() {
        List<SysLineLocationSystemEquipment> locationEquipmentList = sysLineLocationSystemEquipmentMapper.findList(
                new SysLineLocationSystemEquipment());
        CxhtLocationForm locationForm = new CxhtLocationForm();
        locationForm.setIsArchive(0);
        List<CxhtLocationForm> locationFormList = mapper.findList(locationForm);
        // 提取 locationFormList 中所有 equipment ID
        Set<String> formEquipmentIds = locationFormList.stream()
                .map(CxhtLocationForm::getEquipment)
                .filter(Objects::nonNull)
                .map(BeanEntity::getId)
                .collect(Collectors.toSet());

        // 筛选：equipment.getId() 不在 formEquipmentIds 中的项
        List<SysLineLocationSystemEquipment> filteredList = locationEquipmentList.stream()
                .filter(eq -> eq.getId() != null && !formEquipmentIds.contains(eq.getId()))
                .toList();

        filteredList.forEach(locationEquipment -> {
            List<CxhtLocationFormItem> resultLocationFormItem = new ArrayList<>();
            CxhtForm form = cxhtFormMapper.getForItemByEquipment(locationEquipment.getSysEquipment().getId());
            if (form == null) {
                return;
            }
//            CxhtLocationForm cxhtLocationForm = new CxhtLocationForm(locationEquipment.getLineLocation().getId(),
//                    form.getId(), locationEquipment.getId());
//            mapper.insert(cxhtLocationForm);
//            cxhtLocationForm.setId(mapper.getByEquipmentForId(locationEquipment.getId()));
//            form.getCxhtFormItemList().forEach(formItem -> {
//                CxhtLocationFormItem locationFormItem = new CxhtLocationFormItem(cxhtLocationForm.getId(),
//                        formItem.getId());
//                resultLocationFormItem.add(locationFormItem);
//            });
            cxhtLocationFormItemMapper.insertAll(resultLocationFormItem);
        });
    }

    @Transactional()
    public void file() {
        mapper.file();
    }

    @Transactional(readOnly = true)
    public CxhtLocationForm getByIdForItem(String id) {
        CxhtLocationForm cxhtLocationForm = mapper.getByIdForItem(id);
        CxhtForm cxhtForm = cxhtFormMapper.getForItemByEquipment(cxhtLocationForm.getCxhtForm().getEquipment().getId());
        // 创建键值对
        Map<String, CxhtFormItem> map = new HashMap<>();
        cxhtForm.getCxhtFormItemList().forEach(item -> map.put(item.getId(), item));

        cxhtLocationForm.getCxhtLocationFormItemList().forEach(item -> {
            item.setCxhtFormItem(map.get(item.getCxhtFormItem().getId()));
            item.getCxhtFormItem().setCxhtForm(null);
        });
        cxhtLocationForm.setCxhtLocationFormItemTree(
                new CxhtLocationFormItemTree(cxhtLocationForm.getCxhtLocationFormItemList()));
        cxhtLocationForm.setCxhtLocationFormItemList(null);
        return cxhtLocationForm;
    }

    @Transactional
    public void saveItemByTree(CxhtLocationFormItemTree cxhtLocationFormItemTree) {
        cxhtLocationFormItemMapper.batchUpdate(cxhtLocationFormItemTree.toCxhtLocationFormItemList());
    }
}
